package de.smarthome.rooms;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ToggleButton;
import de.smarthome.R;
import de.smarthome.ColorManagement.ColorFragment;
import de.smarthome.ColorManagement.ColorFragment.ColorSetter;
import de.smarthome.controllable.MasterController;
import de.smarthome.database.DatabaseHandler;
import de.smarthome.szenario.ListScenario;

public class RoomFragment extends Fragment implements ColorSetter,
		OnClickListener {

	public DatabaseHandler dbHandler;
	public MasterController masterC;
	public RoomData roomObject;
	public ToggleButton lightButton;
	public ToggleButton curtainButton;
	public Button colorButton;
	public ToggleButton windowButton;
	public ToggleButton blindButton;

	View currentView;
	ToggleButton toggleLight;
	Dialog newScenarioDialog;

	private static final int NEW_SCENARIO = Menu.FIRST;
	private static final int LIST_SCENARIO = NEW_SCENARIO + 1;

	public interface RoomDataProvider {
		RoomData getRoomObject(int index);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		setHasOptionsMenu(true);
		RoomDataProvider dataProvider = (RoomDataProvider) getActivity();
		dbHandler = new DatabaseHandler(getActivity());
		masterC = new MasterController(dbHandler);
		currentView = inflater.inflate(R.layout.room, container, false);
		roomObject = dataProvider.getRoomObject(getArguments().getInt("index"));

		LinearLayout layout = (LinearLayout) currentView
				.findViewById(R.id.roomlayout);

		if (roomObject.hasLight()) {
			layout.addView(inflater.inflate(R.layout.item_light, container,
					false));

			lightButton = (ToggleButton) currentView
					.findViewById(R.id.toggleLight);
			
			if (roomObject.getLight() == 1) {
				lightButton.setChecked(true);
			} else {
				lightButton.setChecked(false);
			}

			lightButton.setOnClickListener(this);

			colorButton = (Button) currentView.findViewById(R.id.colorButton);
			
			getColorForButtons();
			
			colorButton.setOnClickListener(this);
		}

		if (roomObject.hasWindow()) {
			layout.addView(inflater.inflate(R.layout.item_window, container,
					false));
			windowButton = (ToggleButton) currentView
					.findViewById(R.id.toggleWindow);
			
			if (roomObject.getWindow() == 1) {
				windowButton.setChecked(true);
			} else {
				windowButton.setChecked(false);
			}
			windowButton.setOnClickListener(this);

		}

		if (roomObject.hasBlind()) {
			layout.addView(inflater.inflate(R.layout.item_blind, container,
					false));
			blindButton = (ToggleButton) currentView
					.findViewById(R.id.toggleBlind);
			if (roomObject.getBlind() == 1) {
				blindButton.setChecked(false);
			} else {
				blindButton.setChecked(true);
			}
			blindButton.setOnClickListener(this);

		}

		if (roomObject.hasCurtain()) {
			layout.addView(inflater.inflate(R.layout.item_curtain, container,
					false));

			curtainButton = (ToggleButton) currentView
					.findViewById(R.id.toggleCurtain);
			
			if (roomObject.getCurtain() == 1) {
				curtainButton.setChecked(true);
			} else {
				curtainButton.setChecked(false);
			}
			curtainButton.setOnClickListener(this);

		}

		if (roomObject.hasHeater()) {
			layout.addView(inflater.inflate(R.layout.item_heater, container,
					false));
			ToggleButton heater = (ToggleButton) currentView
					.findViewById(R.id.toggleHeater);
			if (roomObject.getHeater() == 1) {
				heater.setChecked(true);
			} else {
				heater.setChecked(false);
			}
		}

		setColorForTouchables(layout);
		setColorForButtons(currentView);
		return currentView;
	}

	@Override
	public void onClick(View v) {
		String room = roomObject.getRoomName();
		System.out.println(room);
		switch (v.getId()) {
		case R.id.toggleLight:

			if (lightButton.isChecked()) {
				masterC.lightOff(room);
			} else {
				masterC.lightOn(room);
			}
			break;
		case R.id.colorButton:
			showDialog();
			break;
		case R.id.toggleCurtain:
			if (curtainButton.isChecked()) {
				masterC.curtainOpen(room);

			} else {
				masterC.curtainClose(room);
			}
			break;
		case R.id.toggleWindow:
			if (windowButton.isChecked()) {
				masterC.windowClose(room);
			} else {
				masterC.windowOpen(room);
			}
			break;
		case R.id.toggleBlind:
			if (blindButton.isChecked()) {
				masterC.blindClose(room);
			} else {
				masterC.blindOpen(room);
			}
			break;
		}
	}

	void showDialog() {
		FragmentTransaction ft = getActivity().getSupportFragmentManager()
				.beginTransaction();
		Fragment prev = getActivity().getSupportFragmentManager()
				.findFragmentByTag("dialog");
		if (prev != null) {
			ft.remove(prev);
		}
		ft.addToBackStack(null);

		DialogFragment newFragment = new ColorFragment();
		newFragment.setTargetFragment(this, 5);
		newFragment.show(ft, "dialog");
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

		super.onCreateOptionsMenu(menu, inflater);
		menu.add(0, NEW_SCENARIO, 0, "create Situation");
		menu.add(0, LIST_SCENARIO, 0, "list Situation(s)");
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case NEW_SCENARIO:
			newScenarioDialog = new Dialog(this.getActivity());
			newScenarioDialog.setContentView(R.layout.dialog_create_scenario);
			newScenarioDialog.setTitle("Create New Situation");

			Button dialogButton = (Button) newScenarioDialog
					.findViewById(R.id.dismissButton);

			dialogButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					EditText name = (EditText) newScenarioDialog
							.findViewById(R.id.newScenarioName);
					String nameOfScenario = name.getText().toString();
					if (!(nameOfScenario.equals(""))) {
						if (dbHandler.containsScenarioName(nameOfScenario)) {
							dbHandler.deleteScenario(nameOfScenario);
						}
						dbHandler.saveStatusForScenario(nameOfScenario);
					}
					newScenarioDialog.dismiss();
				}
			});
			newScenarioDialog.show();
			break;

		case LIST_SCENARIO:

			Intent intent1 = new Intent(getActivity(), ListScenario.class);
			startActivity(intent1);

			break;

		default:
			return super.onOptionsItemSelected(item);

		}
		return true;
	}

	@Override
	public void setColor(int color) {
		dbHandler.updateRoomStats(roomObject.getRoomName(), "color", color);
		masterC.changeColor(roomObject.getRoomName(), color);
		setColorForButtons(currentView, getColorForButtons());

	}

	public int getColorForButtons() {
		return dbHandler.getColor(roomObject.getRoomName());
	}

	public void setColorForButtons(View v) {
		setColorForButtons(v, getColorForButtons());
	}

	public void setColorForButtons(View v, int color) {

		if (color != 0) {
			v.setBackgroundColor(color);
		} else {
			v.setBackgroundColor(Color.DKGRAY);
		}

	}

	public void setColorForTouchables(LinearLayout layout) {
		for (View elem : layout.getTouchables()) {
			if (elem instanceof Button || elem instanceof ToggleButton) {

				elem.setBackgroundColor(Color.BLACK);
				elem.getBackground().setAlpha(125);
			}
		}
	}

}